// pages/index/index.js
const handler=require("../../utils/function.js");
Page({
  /**
   * 页面的初始数据
   */
  data: {
    movies: [
      
    ] ,
    spinShow:true,
    visible1:false,
    actions1: [
      {
        name: '预约课程',
      },
      {
        name: '去分享',
        icon: 'share',
        openType: 'share'
      }
    ],
    currentItemId:"0",
    scrollTop:false,
    title_active:true,
    left:0,
    visible2:false,
    page_country: 1,//国家级导师当前页数
    page_province: 1,//省级导师当前页数
    current_user_type: 0,//用户类型 0、国家级导师 1、省级导师
    tearcher_data: [],//导师数据
    county_data: [],//国家级导师
    province_data: [],//省级导师
    authStatus: true,//授权状态
    canIUse: wx.canIUse('button.open-type.getUserInfo'),//获取登录按钮
    authorize:"http://xyds.4hl.cn/xyds/uploads/otherimg/index/avatar.png"
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.updateUserInfo();
  },
  updateUserInfo:function(){
    var that = this;
    var id = wx.getStorageSync('user_info').id;
    if (id){
      handler.requestData("getUserInfoById", { id: id }, function (res) {
        if (res.success == true) {
          wx.setStorageSync('user_info', res.data);
        } else {
          wx.showToast({
            title: res.message,
            icon: 'loading'
          })
        }
      });
    }
    handler.requestData("bannerList",{},function(res){
      console.log(res.data);
      that.setData({ movies: res.data})
    })
  },
  // 页面登录
  login_test:function(url){
    // console.log(wx.getStorageSync("user_phone"))
    if (wx.getStorageSync("user_phone")===""){
      this.setData({ visible2: true});
    }else{
      handler.intoPageHandler(url);
    }
  },
  handleCloseCancle: function () {
    this.setData({ visible2: false });
  },
  handleCloseOK: function () {
    this.setData({ visible2: false });
    handler.intoPageHandler('../login-enter/index');
  },
  // 页面跳转
  handler_next_page(e){
    this.login_test(e.currentTarget.dataset.url);
    // handler.intoPageHandler(e.currentTarget.dataset.url);
  },
  // 底部导航栏
  handler_next_page_new:function(){
    var user_type = wx.getStorageSync('user_info').user_type;
    if (user_type == 1){
      this.login_test('../teacher-appointment-record/index');
    } else{
      this.login_test('../user-appointment-record/index');
    }
  },
  handleOpen1(e) {
    var user_id = e.currentTarget.dataset.userid;
    this.setData({
      visible1: true,
      tearchid :user_id
    });
     
  },
  handleCancel1() {
    this.setData({
      visible1: false
    });
  },
  onShareAppMessage() {
    return {
      title: 'iView Weapp',
      imageUrl: 'https://file.iviewui.com/iview-weapp-logo.png'
    };
  },
  handleClickItem1(e) {
    //tearchid
    this.login_test("../teacher-detail/index?tearchid="+this.data.tearchid);
    // handler.intoPageHandler("../teacher-detail/index");
  },
  //页面滚动执行方式
  onPageScroll(event){
    event.scrollTop > 164 && this.setData({ scrollTop:true})
    event.scrollTop < 164 && this.setData({ scrollTop: false })
  },
  // into_search_handler 进入搜索页
  into_search_handler:function(e){
    handler.intoPageHandler(e.currentTarget.dataset.url);
  },
  // choose_teacher 选择教师
  choose_teacher:function(e){
    
    var current_btn = e.currentTarget.dataset.type;
    var { title_active, left } = this.data;
    if (current_btn === "county") {
      this.setData({
        title_active: true, left: 0, current_user_type: 0, county_data: this.data.tearcher_data.country_data.data,
        province_data: [], currentItemId:"0"
      });
      console.log(this.data.province_data);
      return;

    } else {
      console.log(this.data.province_data, this.data.tearcher_data.province_data.data,"++++++++++++");
      this.setData({
        title_active: false, left: 750, current_user_type: 1, county_data: [],
        province_data: this.data.tearcher_data.province_data.data,  currentItemId:1
      });
      return;
    }
    
  },
  initData:function(){
    var para = {
      page_country: this.data.page_country,
      page_province: this.data.page_province,
    };
    var that = this;
    handler.requestData("index", para, function (res) {
     
      if (res.success == true) {
        that.setData({
          spinShow: false,
          tearcher_data: res.data,
          county_data: res.data.country_data.data,
          province_data: res.data.province_data.data,
        })
        console.log(that.data.province_data);
        wx.hideNavigationBarLoading(); //完成停止加载
        wx.stopPullDownRefresh(); //停止下拉刷新
      } else {
        wx.showToast({
          title: res.message,
          icon: 'loading'
        })
      }
    });
  },
  //bindsubmithandler
  bindsubmithandler:function(e){
    console.log(e);
  },
  getUserInfo: function (e) {
    var _that = this;
    var detail = e.detail;
    
    if (!detail.iv) {
      wx.showModal({
        title: '提示',
        content: '请点击同意,请重试',
        showCancel: false,
      })
      return false;
    }
    wx.showLoading({
      title: '请稍后',
    })
    wx.login({
      success: function (code) {
        console.log(code);
        var code = code.code;
        wx.getUserInfo({
          success: function (res) {
            //请求接口 保存用户信息
            var para = {code:code,user_info:JSON.stringify(res.userInfo)};
      
            handler.requestData("wechatUser", para, function (res) {
              if (res.success == true) {
                var data = res.data;
                wx.setStorageSync('user_info', data);
                console.log(wx.getStorageSync('user_info'))
              } else {
                wx.showToast({
                  title: res.message,
                  icon: 'loading'
                })
              }
            });
          }
        })
        code !== "" ?( _that.setData({ authStatus: true }), wx.hideLoading(),wx.setStorage({ key: 'access_token',data: code,})):
        (wx.hideLoading(), wx.showToast({title: '请重试',icon: 'none'}))
        }
      })
  },
  closePanel: function () {
    this.setData({
      authStatus: true
    });
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    wx.getStorageSync('access_token')==="" && (this.setData({ authStatus:false}))
    this.initData();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    //下拉刷新
    wx.showNavigationBarLoading() //在标题栏中显示加载
    this.initData();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    //上拉加载更多
    var page_province = this.data.page_province;
    var page_country = this.data.page_country;
    var current_user_type = this.data.current_user_type;
    if (current_user_type == 1){
      var page_province_total = this.data.tearcher_data.province_data.total;
      if (page_province_total < this.data.page_province * 10) {
        console.log("已经是全部数据");
      } else {
        this.setData({
          page_province: page_province + 1
        })
        wx.showNavigationBarLoading() //在标题栏中显示加载
        this.initData();
      }
    }else{
   
      var page_country_total = this.data.tearcher_data.country_data.total;
      if (page_country_total < this.data.page_country * 10) {
        console.log("已经是全部数据");
      } else {
        this.setData({
          page_country: page_country + 1
        })
        wx.showNavigationBarLoading() //在标题栏中显示加载
        this.initData();
      }
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  },
  formSubmit:function(e){
    var user_id = wx.getStorageSync('user_info').id;
    var formid = e.detail.formId;
    // console.log(formid);
    if (user_id && formid){
      
      var para = {
        form_id: formid,
        user_id: user_id
      };
      var that = this;
      var isTrue = e.currentTarget.dataset.istrue;
      if(isTrue == "true"){
        var user_type = wx.getStorageSync('user_info').user_type;
        if (user_type == 1) {
          this.login_test('../teacher-appointment-record/index');
        } else {
          this.login_test('../user-appointment-record/index');
        }

      }else{
        handler.requestData("storageFormid", para, function (res) { console.log(res) });
        that.handler_next_page(e)
      }

   
    }
  }
})